package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name="01-资讯管理模块")
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @Operation( summary = "添加资讯")
    @ApiOperationSupport(order = 1)
    @Schema(description = "添加资讯参数")
    public JsonResult addNotice(NoticeAddParam noticeAddParam) {
        Notice notice = new Notice();
        log.debug("咨询添加"+noticeAddParam);
        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        //添加其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int i = noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }


    @GetMapping("list")
    @Operation( summary = "查询资讯列表")
    @ApiOperationSupport(order = 2)
    public JsonResult list(NoticeListQuery noticeListQuery){
        log.debug("查询参数："+noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList(noticeListQuery);
        return JsonResult.ok(noticeListVOS) ;
    }


    @GetMapping("detail")
    @Operation(summary = "查询资讯详情")
    @ApiOperationSupport(order = 3)
    @Parameters(value = {
            @Parameter(name = "id" ,description = "资讯id", required = true, example = "1"),
            @Parameter(name = "username", description = "用户名", required = true, example = "admin",hidden= true)
            })
    public JsonResult detail(Long id,String username){
        log.debug("查询id："+id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeById(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }



    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 4)
    public JsonResult deleteNoticeById(Long id){
        log.debug("id = "+id);
        int i = noticeMapper.deleteNoticeById(id);
        if(i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }


    @PostMapping("update")
    @Operation(summary = "修改资讯")
    @ApiOperationSupport(order = 5)
    public JsonResult updateNoticeById(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("修改资讯："+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int i = noticeMapper.updateNoticeById(notice);
        if(i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }












}
